#include <stdio.h>
#include <pthread.h>
#include <inttypes.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include "../ufs/url_type_cache.h"

char*		urls[]	= {
	"baidu.com", "google.hk", "hotmail.com", "msn.com",	"msdn.com",
	"csdn.net", "yahoo.com.cn", "sina.com", "163.com", "youku.com",
	NULL
};

uint32_t	gettickcount()
{
	int		fd;
	char	buff[1024]		= {0};

	fd = open("/proc/uptime", O_RDONLY);
	if(-1 == fd)
	{
		return 0;
	}
	read(fd, buff, sizeof(buff));
	close(fd);

	return (uint32_t)(atof(buff) * 1000);
}

void* mythread(void* param)
{
	int			n		= (int)param;
	int			i		= 0;
	
	for(i = 0; NULL != urls[i]; )
	{
		uint32_t	timeout;
		uint8_t		tp1, tp2, tp3, rating;

		usleep(100 * n);
		if(false != utc_query(urls[i], &timeout, &tp1, &tp2, &tp3, &rating))
		{
			printf("%d. thread %d find: %s\n", gettickcount(), n, urls[i]);
			i++;
		}
	}
	return (void *)0;
}


void main(void)
{
	int				i;
	pthread_t		ntid;

	utc_init();

	printf("%d. begin**********\n", gettickcount());
	for(i = 0; i < 5; i++)
	{
		pthread_create(&ntid, NULL, mythread, (void *)(i+1));
	}

	// 定时操作
	for(i = 0; NULL != urls[i]; i++)
	{
		sleep(10);
		
		utc_set(urls[i], 0, 0, 0, 0, 0);
		printf("%d. set url: %s\n", gettickcount(), urls[i]);
	}

	sleep(10);
	printf("%d. **********end\n", gettickcount());
}

